#include <iostream>
#include <string>
#include <unistd.h>
#include <pthread.h>

using namespace std;

__thread int g_val = 100;

string toHex(pthread_t t)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%x", t);
    return id;
}

void *threadRun(void *arg)
{
    cout << "thread: " << toHex(pthread_self()) << " g_val: " << ++g_val << " &g_val: " << &g_val << endl;
    return (void*)0;
}

int main()
{
    pthread_t t[3];
    for(int i = 0; i < 3; i++)
    {
        pthread_create(t + i, nullptr, threadRun, nullptr);
        sleep(1);
    }

    for(int i = 0; i < 3; i++)
        pthread_join(t[i], nullptr);
    return 0;
}
